package edu.mju.scenic.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.mju.scenic.dto.ScenicSpotDto;
import edu.mju.scenic.entity.Spot;
import edu.mju.scenic.service.SpotService;
import edu.mju.scenic.vo.IndexSpotVo;
import edu.mju.scenic.vo.SelectionSpotVo;
import edu.mju.serviceutils.ResponseCode;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CBL
 * @since 2020-12-28
 */
@RestController     //标识Controller
@RequestMapping("/scenic/spot")
//@CrossOrigin   //解决跨域问题
public class SpotController {

    @Autowired
    private SpotService spotService;


    /**
     * 分页带条件查询景点信息
     * @param page 当前页
     * @param size 页面大小
     * @param scenicSpotDto 查询条件
     * @return 自定义返回结果
     */
    @PostMapping("/getSpotList/{page}/{size}")
    public ResponseCode getSpotList(
            //前台注释                                                  前台值传入变量
            @ApiParam(name = "page" ,value = "当前页码" ,required = true) @PathVariable Long page,
            @ApiParam(name = "size" ,value = "页码大小" ,required = true) @PathVariable Long size,
            @ApiParam(name = "searchDto" ,value = "查询条件" ,required = false)@RequestBody(required = false) ScenicSpotDto scenicSpotDto
            ){

        //分页条件放入Page
        IPage<Spot> spotPage = new Page<>(page,size);
        Map<String,Object> map = spotService.getSpotList(spotPage,scenicSpotDto);

        return ResponseCode.success().data(map).data("page",page);
    }


    /**
     *  增加、删除景点信息
     * @param indexSpotVo 前端传入参数
     * @return 自定义返回结果
     */
    @PostMapping("/updateScenicSpot")
    //@RequestBody  把前端传过来的对象封装到这个对象中,,获取json数据封装到body
    public ResponseCode updateScenicSpot(@RequestBody IndexSpotVo indexSpotVo){
        spotService.saveOrUpdateSpot(indexSpotVo);
        return ResponseCode.success();
    }


    /**
     *  根据id删除景点
     * @param id 景点id
     * @return 自定义返回结果
     */
    @DeleteMapping("/deleteScenicSpot/{id}")
    public ResponseCode deleteScenicSpot(@ApiParam(name = "id" ,value = "删除景点id" ,required = true) @PathVariable Integer id){
        int i = spotService.deleteScenicSpot(id);
        if(i==0){
            return ResponseCode.error().message("删除失败！");
        }
        return ResponseCode.success();
    }

    /**
     * 根据景点 查询scenic_spot、scenic_img、scenic_celebrity、scenic_attractions
     *
     */
    @GetMapping("/getScenicSpotAllInfo/{id}")
    public ResponseCode getScenicSpotAllInfo(@ApiParam(name = "id" ,value = "景点id" ,required = true) @PathVariable Integer id){
        Map<String,Object> map = spotService.getScenicSpotAllInfo(id);
        return ResponseCode.success().data(map);
    }

    /**
     * 供远程调用接口 根据id查询景点信息
     * @param id 景点id
     * @return 封装的数据
     */
    @GetMapping("/getSpotInfo/{id}")
    public Spot getSpotInfo(@ApiParam(name = "id" ,value = "景点id" ,required = true) @PathVariable Integer id){
        return spotService.getById(id);
    }

    /**
     * 供前端调用接口 根据id查询景点信息
     * @param id 景点id
     * @return 封装的数据
     */
    @GetMapping("/getSpotInfoFront/{id}")
    public ResponseCode getSpotInfoFront(@ApiParam(name = "id" ,value = "景点id" ,required = true) @PathVariable Integer id){
        Spot byId = spotService.getById(id);
        return ResponseCode.success().data("spotInfo",byId);
    }


    /**
     * 供前端查询景点id和景点name
     */
    @GetMapping("/getSpot")
    public ResponseCode getSpot(){
        QueryWrapper<Spot> wrapper = new QueryWrapper<>();
        List<Spot> list = spotService.list(wrapper);
        List<SelectionSpotVo> selectionSpotVoList = new ArrayList<>();
        for (Spot spot : list) {
            SelectionSpotVo selectionSpotVo = new SelectionSpotVo();
            BeanUtils.copyProperties(spot,selectionSpotVo);
            selectionSpotVoList.add(selectionSpotVo);
        }

        return ResponseCode.success().data("spotList",selectionSpotVoList);
    }
}

